// pages/welcome/index.js
var postData = require('../../datas/posts-data.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    post_key: [

    ],
    searchKey:"",
    searchType:"",
    isFromSearch: true,   // 用于判断searchSongList数组是不是空数组，默认true，空的数组  
    searchPageNum: 1,   // 设置加载的第几次，默认是第一次  
    callbackcount: 15,      //返回数据的个数  
    searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
    searchLoadingComplete: false,  //“没有数据”的变量，默认false，隐藏  
    scrollHeight: 0,
    scrollTop:0,
    scrollTop2: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    console.log(this.data.scrollHeight);
    that.setData({
      searchKey: options.key,
      searchType: options.type
    });
    this.fetchSearchList();  
  },
  //下拉刷新
  onPullDownRefresh: function () {
    console.log("hhh");
    wx.showNavigationBarLoading() //在标题栏中显示加载

    //模拟加载
 
      // complete
     // wx.hideNavigationBarLoading() //完成停止加载
     // wx.stopPullDownRefresh() //停止下拉刷新

  },
  //滚动到底部触发事件  
  searchScrollLower: function () {
    var that = this;
    console.log("上拉加载" + that.data.searchLoading);
    console.log(that.data.searchLoadingComplete);
    if (!that.data.searchLoading && !that.data.searchLoadingComplete) {
      that.setData({
        searchPageNum: that.data.searchPageNum + 1,  //每次触发上拉事件，把searchPageNum+1  
        isFromSearch: false,  //触发到上拉事件，把isFromSearch设为为false 
        searchLoading:true
      });
      that.fetchSearchList();
    }
  } ,
  scroll: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      scrollTop2: event.detail.scrollTop
    });
  },
  fetchSearchList: function () {
    var that = this
    console.log('https://www.huangliusong.top/XCX/datas?key=' + that.data.searchKey + "&type=" + that.data.searchType + "&searchPageNum=" + this.data.searchPageNum + "&callbackcount=" + this.data.callbackcount);
    wx.request({
      url: 'https://www.huangliusong.top/XCX/datas?key=' + that.data.searchKey + "&type=" + that.data.searchType + "&searchPageNum=" + this.data.searchPageNum + "&callbackcount=" + this.data.callbackcount, //仅为示例，并非真实的接口地址,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var post_contents1 = res.data;

        that.setData({
          post_key: post_contents1,
           searchLoading: false,
           scrollTop: that.data.scrollTop2
        });
        console.log(post_contents1)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})